﻿using System.Diagnostics;

namespace ctolua.Models
{
    /// <summary>
    /// 单位组
    /// </summary>
    public class MUnitGroup
    {
        /// <summary>
        /// 单位组
        /// </summary>
        public UnitGroup unitGroup { get; set; }
        /// <summary>
        /// 是否存在
        /// </summary>
        public bool? is_null => game_api.unit_group_is_exist(this.unitGroup);

        public MUnitGroup(UnitGroup unitGroup)
        {
            this.unitGroup = unitGroup;
        }

        public static MUnitGroup obj(UnitGroup unitGroup)
        {
            return new MUnitGroup(unitGroup);
        }

        /// <summary>
        /// 单位组转单位
        /// </summary>
        /// <returns></returns>
        public List<Unit> pick()
        {
            
            List<Unit> lua_table = new List<Unit>();
            for (int i = 0; i < py.python_len(this.unitGroup); i++)
            {
                Unit iter_unit = game_api.get_unit_by_id(py.python_index(this.unitGroup, i));
                lua_table.Add(iter_unit);
            }
            return lua_table;
        }
        /// <summary>
        /// 添加单位到单位组
        /// </summary>
        /// <param name="unit"></param>
        public void add(Unit unit)
        {
            game_api.add_unit_to_group(unit, this.unitGroup);
        }
        /// <summary>
        /// 删除单位组中指定编号的单位
        /// </summary>
        /// <param name="key"></param>
        public void del_key(int key)
        {
            game_api.remove_unit_in_group_by_key(this.unitGroup, key);
        }
        /// <summary>
        /// 删除单位组某个单位
        /// </summary>
        /// <param name="unit"></param>
        public void del(Unit unit) => game_api.remove_unit_in_group(this.unitGroup, unit);
        /// <summary>
        /// 设置单位组中单位选中
        /// </summary>
        public void set_unit_group_selected() => game_api.set_unit_group_selected(this.unitGroup);
        /// <summary>
        /// 单位组某状态的单位数量
        /// </summary>
        /// <param name="status ">  状态名称   </param>
        /// <returns>单位数量</returns>
        public int? get_state_unit_num_in_group(string status) => game_api.get_state_unit_num_in_group(this.unitGroup, status);
        /// <summary>
        /// 单位组中特定单位编号的单位数量
        /// </summary>
        /// <param name="unit_key ">  单位编号   </param>
        public int? get_num_of_unit_key_in_group(int unit_key) => game_api.get_num_of_unit_key_in_group(this.unitGroup, unit_key);
        /// <summary>
        /// 遍历时过滤单位组
        /// </summary>
        /// <param name="unit_group ">  单位组   </param>
        /// <returns>单位组</returns>
        public MUnitGroup refresh_unit_group(UnitGroup unitGroup) => new MUnitGroup(game_api.refresh_unit_group(unitGroup));
        /// <summary>
        /// 单位组内所有状态的单位数量
        /// </summary>
        /// <returns>单位数量</returns>
        public int? count => game_api.get_unit_group_num(this.unitGroup);
        /// <summary>
        /// 筛选范围内单位
        /// </summary>
        /// <param name="pos ">  坐标   </param>
        /// <param name="shape ">  形状   </param>
        /// <param name="belong_role_group ">  属于玩家或玩家组   </param>
        /// <param name="visible_role ">  对玩家可见   </param>
        /// <param name="invisible_role ">  对玩家不可见   </param>
        /// <param name="exclude_unit_group ">  不在单位组内   </param>
        /// <param name="with_tag ">  具有标签   </param>
        /// <param name="without_tag ">  不具有标签   </param>
        /// <param name="entity_no ">  单位名称   </param>
        /// <param name="exclude_unit ">  排除单位   </param>
        /// <param name="unit_type ">  单位种类   </param>
        /// <param name="in_state ">  具有状态   </param>
        /// <param name="not_in_state ">  不具有状态   </param>
        /// <param name="include_dead ">  是否包括死亡单位   </param>
        /// <param name="max_count ">  数量上限   -1</param>
        /// <param name="sort_type ">  排序类型   0 由近到远 1由远到近 2随机筛选 默认0 </param>
        /// <returns>单位组</returns>
        public static MUnitGroup filter_unit_id_list_in_area_v2(Point pos, Shape shape, Role belong_role_group=null, Role visible_role=null, Role invisible_role=null, UnitGroup exclude_unit_group=null, string with_tag=null, string without_tag=null, int? entity_no = 0, Unit exclude_unit=null, int? unit_type=null, int? in_state=0, int? not_in_state=0, bool? include_dead=false, int? max_count=999, int? sort_type=0) =>
            new MUnitGroup(game_api.filter_unit_id_list_in_area_v2(pos, shape, belong_role_group, visible_role, invisible_role, exclude_unit_group, with_tag, without_tag, entity_no, exclude_unit, unit_type, in_state, not_in_state, include_dead, max_count, sort_type));
        /// <summary>
        /// 随机一个单位
        /// </summary>
        public Unit random => game_api.get_random_unit_in_unit_group(this.unitGroup);
        /// <summary>
        /// 第一个单位
        /// </summary>
        public Unit first => game_api.get_first_unit_in_group(this.unitGroup);
        /// <summary>
        /// 最后一个单位
        /// </summary>
        public Unit last => game_api.get_last_unit_in_group(this.unitGroup);
    }
}
